﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Entidades.Reclamos;
using WebApplication.Reclamos;
using System.Text;
using System.IO;

public partial class frmConsultaReclamo : System.Web.UI.Page
{
#region ATRIBUTOS

    protected IControllerRecReclamo _recController;
    protected DataTable dt {
                    get{
                        if(ViewState["dt_Reclamo"]==null){
                            ViewState["dt_Reclamo"] = new DataTable();
                        }
                        return (DataTable)ViewState["dt_Reclamo"];
                    }
                    set{
                        ViewState["dt_Reclamo"] = value;
                    }
    }
    protected RecReclamo criterio;

    #endregion

    #region CONSTRUCTOR

    public frmConsultaReclamo()
    {
        _recController = new ControllerRecReclamo();
        criterio = new RecReclamo();
    }

    #endregion

    #region EVENTOS DEL FORMULARIO

    protected void Page_Load(object sender, EventArgs e)
    {
        verMensajes();
        if (!IsPostBack)
        {
            txtFechaInicio.Text = "01/01/2013";
            txtFechaFin.Text = String.Format("{0:dd/MM/yyyy}", DateTime.Now); ;
       
        }
    }




    protected void verMensajes() {
        String msg = (Session["reclamo_rpta"] == null) ? "" : Session["reclamo_rpta"].ToString();        
        switch (msg) { 
            case "1":                
                Page.ClientScript.RegisterStartupScript(this.GetType(),"MSG_SAVE", "alert('" + Constantes.MSG_SAVE + "')", true);
                Session["reclamo_rpta"] = null;
                break;
            case "2":                
                Page.ClientScript.RegisterStartupScript(this.GetType(), "MSG_DELETE", "alert('" + Constantes.MSG_DELETE + "')", true);
                Session["reclamo_rpta"] = null;
                break;
            case "3":
                String msg_detail = (Session["reclamo_rpta_detail"] == null) ? "" : Session["reclamo_rpta_detail"].ToString();            
                msg_detail = msg_detail.Replace("\"","");
                msg_detail = msg_detail.Replace("'", "");
                msg_detail = msg_detail.Replace("\n", " ");
                msg_detail = msg_detail.Replace("\r", " ");
                Page.ClientScript.RegisterStartupScript(this.GetType(), "MSG_EXCEPTION1", "$('#mensaje').html('" + msg_detail + "'); ", true);
                //Page.ClientScript.RegisterStartupScript(this.GetType(), "MSG_EXCEPTION2", "$(document).ready(function(){tb_show('mensaje', '#TB_inline?height=240&amp;width=405&amp;inlineId=hiddenModalContent&amp;modal=true', null);}", true);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "MSG_EXCEPTION2", "$(document).ready(function(){tb_show('" + Constantes.MSG_EXCEPTION +"', '#TB_inline?height=240&amp;width=405&amp;inlineId=hiddenModalContent&amp;modal=true', null);}", true);

                Session["reclamo_rpta"] = null;
                Session["reclamo_rpta_detail"] = null;


                break;
            default: 
                break;
        }        
    }

    protected void exportar()
    {
        List<RecReclamo> lista = _recController.listarReclamoSolucionados(criterio);

        if (lista.Count > 0)
        {
            string attachment = "attachment; filename=HRPendientes.xls";
            Response.ClearContent();
            Response.Charset = "UTF-8";
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/vnd.ms-excel";
            Response.ContentEncoding = Encoding.Default;

            System.Web.UI.Page page1 = new System.Web.UI.Page();
            StringBuilder builder1 = new StringBuilder();
            StringWriter writer1 = new StringWriter(builder1);
            HtmlTextWriter writer2 = new HtmlTextWriter(writer1);
            HttpContext context = HttpContext.Current;
            string cuerpoExcel = string.Empty;


            String mHeader = String.Empty;
            mHeader = "<table>";
            mHeader = mHeader + "<tr>";
            mHeader = mHeader + "<td colspan=13 style='font-weight:bold;font-size: 26pt;' align='center'>HOJA DE RECLAMACIONES PENDIENTES</td>";
            mHeader = mHeader + "</tr>";
            mHeader = mHeader + "<tr>";
            mHeader = mHeader + "<td style='font-weight:bold;font-size: 10pt;'></td>";
            mHeader = mHeader + "</tr>";
            mHeader = mHeader + "</table>";

            StringBuilder mBody = new StringBuilder();
            mBody.Append("<tr>");
            mBody.Append("<td style='text-align:center;background-color:#D8D8D8;font-weight:bold' >CODIGO</td>");
            mBody.Append("<td style='text-align:center;background-color:#D8D8D8;font-weight:bold'>LOCAL</td>");
            mBody.Append("<td style='text-align:center;background-color:#D8D8D8;font-weight:bold'>NOMBRES</td>");
            mBody.Append("<td style='text-align:center;background-color:#D8D8D8;font-weight:bold'>DESCRIPCION</td>");
            mBody.Append("</tr>");
            foreach (RecReclamo be in lista)
            {

                mBody.Append("<tr>");
                mBody.Append("<td style='text-align:center;' >" + be.IdReclamo.ToString() + "</td>");
                mBody.Append("<td style='text-align:center;'>" + be.Local.Nombre.ToString() + "</td>");
                mBody.Append("<td style='text-align:center;'>" + be.Nombres.ToString() + "</td>");
                mBody.Append("<td style='text-align:center;'>" + be.Descripcion + "</td>");
                mBody.Append("</tr>");
            }

            writer2.Write(mHeader + mBody.ToString());
            page1.DesignerInitialize();
            page1.RenderControl(writer2);
            page1.Dispose();
            page1 = null;
            Response.Write(builder1.ToString());
            Response.End();

        }
        else
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "MSG_EMPTY", "alert('No hay registros disponibles para impresión')", true);

        }

    }


    protected void Listar() {
        List<RecReclamo> lista = _recController.listarReclamoSolucionados(criterio);
        dt = new DataTable();
        dt.Columns.Add("Código");
        dt.Columns.Add("Local");
        dt.Columns.Add("Nombres");
        dt.Columns.Add("Descripción");
        
        DataRow dr;
        foreach (RecReclamo be in lista)
        {
            dr = dt.NewRow();
            dr[0] = be.IdReclamo.ToString();
            dr[1] = be.Local.Nombre.ToString();
            dr[2] = be.Nombres.ToString();            
            dr[3] = be.Descripcion.ToString();
            dt.Rows.Add(dr);    
        }

        gvCargos.DataSource = dt;
        gvCargos.DataBind();
    }

    #endregion

    #region EVENTOS DE BOTONES



    protected void btnImprimir_Click(object sender, EventArgs e)
    {
        System.Globalization.CultureInfo cultura = new System.Globalization.CultureInfo("en-US");
        criterio = new RecReclamo();
        criterio.IdReclamo = (txtNumero.Text.Trim().Equals("")) ? 0 : Int32.Parse(txtNumero.Text);
        criterio.Nombres = txtNombres.Text;
        criterio.FecInicio = (txtFechaInicio.Text != "" ? DateTime.Parse(txtFechaInicio.Text, cultura.DateTimeFormat) : DateTime.MinValue.Date);
        criterio.FlInicio = ((txtFechaInicio.Text != "" ? DateTime.Parse(txtFechaInicio.Text, cultura.DateTimeFormat) : DateTime.MinValue.Date) != DateTime.MinValue.Date);
        criterio.FecFin = (txtFechaFin.Text != "" ? DateTime.Parse(txtFechaFin.Text, cultura.DateTimeFormat) : DateTime.MinValue.Date);
        criterio.FlFin = ((txtFechaFin.Text != "" ? DateTime.Parse(txtFechaFin.Text, cultura.DateTimeFormat) : DateTime.MinValue.Date) != DateTime.MinValue.Date);
        
        exportar(); 
    }

    protected void btnBuscar_Click(object sender, EventArgs e)
    {
        System.Globalization.CultureInfo cultura = new System.Globalization.CultureInfo("en-US");
        criterio = new RecReclamo();
        criterio.IdReclamo = (txtNumero.Text.Trim().Equals("")) ? 0 : Int32.Parse(txtNumero.Text);
        criterio.Nombres = txtNombres.Text;
        criterio.FecInicio = (txtFechaInicio.Text != "" ? DateTime.Parse(txtFechaInicio.Text, cultura.DateTimeFormat) : DateTime.MinValue.Date);
        criterio.FlInicio = ((txtFechaInicio.Text != "" ? DateTime.Parse(txtFechaInicio.Text, cultura.DateTimeFormat) : DateTime.MinValue.Date) != DateTime.MinValue.Date);
        criterio.FecFin = (txtFechaFin.Text != "" ? DateTime.Parse(txtFechaFin.Text, cultura.DateTimeFormat) : DateTime.MinValue.Date);
        criterio.FlFin = ((txtFechaFin.Text != "" ? DateTime.Parse(txtFechaFin.Text, cultura.DateTimeFormat) : DateTime.MinValue.Date) != DateTime.MinValue.Date);
        
        Listar(); 
    }

    #endregion

    #region EVENTOS DE GRID

    protected void gvCargos_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvCargos.PageIndex = e.NewPageIndex;
        gvCargos.DataSource = dt;
        gvCargos.DataBind();
    }

    protected void verDetalle_OnClick(object sender, CommandEventArgs e)
    {
        string codigo = e.CommandName.ToString();
        Session["reclamo_codigo"] = codigo;
        Session["reclamo_action"] = "visualizar";
        Session["reclamo_visible"] = false;
        Response.Redirect("frmReclamo.aspx");
    }

  

    #endregion

}